package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.EcElementCategoryRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.EcElementCategory;
import com.ndp.fb.rdb.model.User;

import java.util.List;

/**
 * Created by jacky.cheng on 2016/5/31.
 */
@Service
public class EcElementCategoryRdbServiceImpl extends DaoTemplate implements EcElementCategoryRdbService {
    @Override
    public List<EcElementCategory> findAll() {
        return findAll(DefaultRowMapper.autoRowMapper(EcElementCategory.class));
    }

    @Override
    public Long save(EcElementCategory obj) {
        return saveResultId(obj);
    }

    @Override
    public void update(EcElementCategory obj) {
        String sql  = "update ec_element_category set context = ? where id = ? ";
        getJdbcTemplate().update(sql,new Object[]{obj.getContext(),obj.getId()});
    }

    @Override
    public List<EcElementCategory> findCnByMaterielId(Long id) {
        String sql = "SELECT ee.*\n" +
                " from ec_element_category ee, ec_materiel_elements em\n" +
                "where ee.`code` = em.parent_code\n" +
                "and em.ec_template_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{id}, DefaultRowMapper.autoRowMapper(EcElementCategory.class));
    }

    @Override
    public List<EcElementCategory> findUsByMaterielId(Long id) {
        String sql = "SELECT ee.*\n" +
                " from ec_element_category ee, ec_materiel_elements em\n" +
                "where ee.`code` = em.parent_code\n" +
                "and em.ec_template_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{id}, DefaultRowMapper.autoRowMapper(EcElementCategory.class));
    }

    @Override
    public List<EcElementCategory> findTopCategory(Long id1) {
        return null;
    }

    @Override
    public List<EcElementCategory> findBigCatagoryCN() {
        // 需要出过一些特定元素
        String sql = "SELECT * FROM ec_element_category WHERE pid = 0 AND support_element_filter = 1";
        return getJdbcTemplate().query(sql, DefaultRowMapper.autoRowMapper(EcElementCategory.class));
    }

    @Override
    public List<EcElementCategory> findBigCatagoryUS() {
        // 需要出过一些特定元素t.en_tag_name AS tag_name, t.*
        String sql = "SELECT t.context AS name, t.* FROM ec_element_category t WHERE t.pid = 0 AND t.support_element_filter = 1";
        return getJdbcTemplate().query(sql, DefaultRowMapper.autoRowMapper(EcElementCategory.class));
    }

    @Override
    public List<EcElementCategory> findSmallCatagoryCN(Long elementType) {
        StringBuffer sql = new StringBuffer("SELECT * FROM ec_element_category WHERE support_element_filter = 1");
        if(elementType == 0L) {
            sql.append(" AND pid <> 0");
            return getJdbcTemplate().query(sql.toString(), DefaultRowMapper.autoRowMapper(EcElementCategory.class));
        } else {
            sql.append(" AND pid = ?");
            return getJdbcTemplate().query(sql.toString(), new Object[]{elementType}, DefaultRowMapper.autoRowMapper(EcElementCategory.class));
        }
    }

    @Override
    public List<EcElementCategory> findSmallCatagoryUS(Long elementType) {
        StringBuffer sql = new StringBuffer("SELECT t.context AS name, t.* FROM ec_element_category t WHERE t.support_element_filter = 1");
        if(elementType == 0L) {
            sql.append(" AND t.pid <> 0");
            return getJdbcTemplate().query(sql.toString(), DefaultRowMapper.autoRowMapper(EcElementCategory.class));
        } else {
            sql.append(" AND t.pid = ?");
            return getJdbcTemplate().query(sql.toString(), new Object[]{elementType}, DefaultRowMapper.autoRowMapper(EcElementCategory.class));
        }
    }

    @Override
    public List<EcElementCategory> allCreate() {

        String sql = "SELECT * FROM `ec_element_category` e where e.support_materiel = 1";

        return getJdbcTemplate().query(sql,DefaultRowMapper.autoRowMapper(EcElementCategory.class));
    }

    @Override
    public EcElementCategory findByCode(String code) {
        String sql = "select * from ec_element_category where code = ?";
        return findOne(sql, new Object[]{code}, DefaultRowMapper.autoRowMapper(EcElementCategory.class));
    }
}
